import { store } from "@/store"

export interface article {
  id: number
  content: string
  title: string
  intro: string
  createTime: string
  category: string
  img?: string
  updateTime?: string
  tag?: string | Array<string>
}

interface dataArrayItem {
  id: number
  data: string
}

let articles: Array<article> = []

export const pageSize = 6 as const

export function formatSingleData(content: dataArrayItem) {
  const { data, id } = content
  const articleInfoArray = data.split("|||")
  const articleContent = articleInfoArray[1]
  const articleSetting = articleInfoArray[0]
  const el = document.createElement("div")
  const defaultImg = "https://fuyouplus.oss-cn-beijing.aliyuncs.com/blog-imgs/21.jpg"
  let title = ""
  let intro = ""
  let createTime = ""
  let category = ""
  let img = ""
  let updateTime = ""
  let tag: string | Array<string> = []
  el.innerHTML = articleSetting
  title = el.querySelector("blog-title").textContent
  intro = el.querySelector("blog-intro").textContent
  createTime = el.querySelector("blog-createTime").textContent
  category = el.querySelector("blog-category").textContent
  img = el.querySelector("blog-img").textContent || defaultImg
  updateTime = el.querySelector("blog-updateTime").textContent
  const tags = el.querySelectorAll("blog-tag")
  if (tags.length) {
    tags.forEach(item => {
      ; (tag as Array<string>).push(item.textContent)
    })
    tag = tag.join("|")
  }
  return {
    id,
    content: articleContent,
    title,
    intro,
    createTime,
    category,
    img,
    updateTime,
    tag,
  }
}


function formatData(dataArray: Array<dataArrayItem>) {
  const data = dataArray.map(item => formatSingleData(item))
  // articles = articles.concat(data as Array<article>)
  data.sort((a, b) => b.id - a.id)
  store.commit("setArticles", data)
}

// let count = 1

// function createTaskSlices(start, end) {
//   const tasks = []
//   for (let i = start; i <= end; i++) {
//     tasks.push(new Promise(async (resolve, reject) => {
//       let file = null

//       // const content = (await import(`/articles/${i}.md`)).default
//       // file = {
//       //   id: i,
//       //   data: content,
//       // }
//       const filesObj = await import.meta.glob<{ default: string }>(`/articles/*.md`)
//       // const filesObj = await import.meta.glob(`/articles/1.md`, { eager: true })
//       // console.log(filesObj);

//       for (let fileName in filesObj) {
//         const content = await filesObj[fileName]()
//         const id = /\d+/.exec(fileName)[0]

//         file = {
//           id: Number(id),
//           data: content.default,
//         }
//       }
//       resolve(file)
//     }))
//   }
//   return tasks
// }


async function firstRenderArticle() {
  window.$loading()
  const dataArray: Array<dataArrayItem> = []
  const filesObj = await import.meta.glob<{ default: string }>(`/articles/first/*.md`)
  for (let fileName in filesObj) {
    const content = await filesObj[fileName]()
    const id = /\d+/.exec(fileName)[0]
    dataArray.push({
      id: Number(id),
      data: content.default,
    })
  }
  formatData(dataArray)
  window.$closeLoading()
}

async function endRenderArticle() {
  const dataArray: Array<dataArrayItem> = []
  const filesObj = await import.meta.glob<{ default: string }>(`/articles/end/*.md`)
  for (let fileName in filesObj) {
    const content = await filesObj[fileName]()
    const id = /\d+/.exec(fileName)[0]

    dataArray.push({
      id: Number(id),
      data: content.default,
    })
  }
  formatData(dataArray)
}

async function getAllArticles() {
  await firstRenderArticle()
  await endRenderArticle()

  // const filesObj = await import.meta.glob<{ default: string }>("/articles/*.md")
  // for (let fileName in filesObj) {
  //   const content = await filesObj[fileName]()
  //   const id = /\d+/.exec(fileName)[0]

  //   dataArray.push({
  //     id: Number(id),
  //     data: content.default,
  //   })
  //   count++
  // }
  // dataArray.sort((a, b) => b.id - a.id)
  // formatData(dataArray)

}

if (import.meta.hot) {
  import.meta.hot.on("article-update", data => {
    // 执行自定义更新
    store.commit("updateArticle", data)
  })
}

export function useArticles() {
  getAllArticles()
}
